package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
//展示具体的item的界面
public class ViewItemServlet extends HttpServlet {
    private static final String VIEW_ITEM = "/WEB-INF/jsp/catalog/Item.jsp";
    private String itemId;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        itemId = request.getParameter("itemId");
        //利用业务逻辑层获得所需东西
        CatalogService catalogService = new CatalogService();
        //给你id,得到item
        Item item = catalogService.getItem(itemId);
        //把获得的数据放到作用域里存起来
        HttpSession session = request.getSession();
        session.setAttribute("item",item);

        Account account = (Account) session.getAttribute("account");

        if(account!=null){

            //时间
            SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String datetime = tempDate.format(new java.util.Date());
            //信息
            String info = "查看"+itemId+"界面";
            Log log = new Log(account.getUsername(),datetime,info);
            //添加log
            new AccountService().addLog(log);

        }

        request.getRequestDispatcher(VIEW_ITEM).forward(request,response);

    }
}
